package han.cirno.corrupt.modules.score

import androidx.activity.compose.LocalActivity
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedCard
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import han.cirno.corrupt.compose.localeStringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import han.cirno.corrupt.R
import han.cirno.corrupt.activity.main.MainActivityViewModel
import han.cirno.corrupt.compose.SimpleTextPair
import han.cirno.corrupt.compose.VerticalSpacer
import han.cirno.corrupt.data.instances.UserScoreData

@Composable
fun ScoreLayout()=Column(modifier = Modifier
    .fillMaxSize()
    .padding(horizontal = 16.dp)
    .verticalScroll(rememberScrollState())) {
    val mainActivityViewModel:MainActivityViewModel= hiltViewModel(LocalActivity.current as AppCompatActivity)
    val scores by mainActivityViewModel.allScoresFlow.collectAsState()
    scores.forEach {
        ScoreCard(it)
    }
}

@Composable
fun ScoreCard(score: UserScoreData.Score){
    val errorBrush = Brush.linearGradient(
        colors = listOf(MaterialTheme.colorScheme.error,MaterialTheme.colorScheme.error),
    )
    val apBrush = Brush.linearGradient(
        colors = listOf(MaterialTheme.colorScheme.primary,MaterialTheme.colorScheme.primary),
    )
    val floatScore=score.score.toFloatOrNull()
    var show by remember { mutableStateOf(false) }
    OutlinedCard(
        modifier=Modifier
            .fillMaxWidth()
            .padding(0.dp, 4.dp),
        border = CardDefaults.outlinedCardBorder().let {
            if(floatScore==null||floatScore in 60f..<100f)return@let it
            it.copy(brush =
            when{
                floatScore==100f->apBrush
                floatScore<60f ->errorBrush
                else->apBrush
            })
        },
        onClick = { show=!show }
    ) {
        Column(modifier = Modifier.padding(16.dp)) {
            Row(verticalAlignment = Alignment.CenterVertically) {
                Text(
                    text=score.name, style = MaterialTheme.typography.titleMedium,
                    textAlign = TextAlign.Start)
                if(score.optional)
                    Text(
                        text= localeStringResource(R.string.score_optional),
                        style = MaterialTheme.typography.titleSmall,
                        textAlign = TextAlign.Start)
                Text(
                    modifier = Modifier.weight(1f),
                    text=score.score,
                    style = MaterialTheme.typography.titleLarge.copy(
                        brush = when{
                            floatScore==null->null
                            floatScore==100f->apBrush
                            floatScore<60f->errorBrush
                            else->null
                        }
                    ), textAlign = TextAlign.End)
            }
            AnimatedVisibility(visible = show) {
                Column(modifier = Modifier.fillMaxWidth()) {
                    VerticalSpacer(8.dp)
                    SimpleTextPair(localeStringResource(R.string.score_gp), score.gp)
                    SimpleTextPair(localeStringResource(R.string.score_credits), score.credits)
                    SimpleTextPair(
                        localeStringResource(R.string.score_info),
                        "${score.place}/${score.property}/${score.belonging}"
                    )
                    SimpleTextPair(
                        localeStringResource(R.string.score_note),
                        "${score.note}/${score.reExamNote}"
                    )
                    SimpleTextPair(
                        localeStringResource(R.string.score_year_term),
                        "${score.year}/${score.term}"
                    )
                    SimpleTextPair(
                        localeStringResource(R.string.score_resit_score),
                        score.reExamScore.ifEmpty { localeStringResource(R.string.score_nullity) }
                    )
                    SimpleTextPair(
                        localeStringResource(R.string.score_rebuild_status),
                        score.isReBuild.ifEmpty { localeStringResource(R.string.score_nullity) }
                    )
                    SimpleTextPair(
                        localeStringResource(R.string.score_code),
                        score.code
                    )
                }
            }
        }
    }
}